<template>
  <div>
    <Card shadow>
        <iview-table
            ref="table"
            :baseURL="queryModel.baseURL"
            :menuId="dataModel.menuId"
            :columns="dataModel.columns"
            :order="dataModel.order"
            :pageNumber="pageModel.pageNumber"
            :pageSize="pageModel.pageSize"
            :defaultFilters="queryModel.defaultFilters"
            v-on="{changeSelect:changeSelect,changeTotalSize:changeTotalSize,events:events}">
        </iview-table>
        <br>
        <iview-page 
            :totalSize="pageModel.totalSize"
            :pageNumber="pageModel.pageNumber" 
            :pageSize="pageModel.pageSize"
            :pageSizeOpts="pageModel.pageSizeOpts"
            v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
        </iview-page>
    </Card>
  </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
export default {
    components: {
        iviewTable,
        iviewPage
    },
    data () {
        return {
            baseURL: '/user/patient',
            selectRows: [],
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/patient',
                defaultFilters: [
                ]
            },
            dataModel: {
                menuId: 500,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '患者姓名',
                        key: 'name'
                    },{
                        title: '病历号',
                        key: 'medicalId'
                    },{
                        title: '患者昵称',
                        key: 'nickName'
                    },{
                        title: '性别',
                        key: 'sex',
                        render: (h, params) => {
                            let temp = params.row.sex
                            if (temp === 1) {
                                temp = '男'
                            } else if (temp === 2) {
                                temp = '女'
                            }
                            return temp
                        }
                    },{
                        title: '出生日期',
                        key: 'birthday',
                        type: 'Date'
                    },{
                        title: '手机号码',
                        key: 'phone'
                    },{
                        title: '微信号码',
                        key: 'weChat'
                    },{
                        title: '现居住地',
                        key: 'address'
                    }
                ],
                order: 'creation_time desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            }
        }
    },
    methods: {
        events (item) {
            if (item.path === 'add') {
                this.$router.push({
                    name: 'work_patient_add'
                })
            } else if (item.path === 'delete') {
                this.delete()
            }
        },
        delete () {
            if (this.selectRows.length > 0) {
                this.$Modal.confirm({
                    title: '操作提示',
                    content: '<p>确认删除?</p>',
                    onOk: () => {
                        let ids = []
                        for (let row of this.selectRows) {
                            ids.push(row.id)
                        }
                        let url = this.baseURL + '/delete'
                        this.$ajax.post(url, ids).then((response) => {
                            let {code, msg} = response.data
                            if (code === 0) {
                                this.$bus.alert('success', msg)
                                this.refresh()
                            } else {
                                this.$bus.alert('error', msg)
                            }
                        })
                    }
                })
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        refresh () {
            this.$refs.table.refresh()
        },
        changeSelect (selection) {
            this.selectRows = selection
        },
        changeTotalSize (totalSize) {
            this.pageModel.totalSize = totalSize
        },
        changePageNumber (pageNumber) {
            this.pageModel.pageNumber = pageNumber
        },
        changePageSize (pageSize) {
            this.pageModel.pageSize = pageSize
        },
        init () {
            this.refresh()
        }
    },
    mounted () {
        this.init()
    },
    watch: {
        '$route' (to) {
            if (to.path === '/patient') {
                this.init()
            }
        }
    }
}
</script>

<style>

</style>
